package LeetCode;

import java.util.ArrayDeque;
import java.util.Deque;

public class Code98_isValidBST {

    public boolean isValidBST(TreeNode root) {
        if (root == null){
            return true;
        }
        Deque<TreeNode> stack = new ArrayDeque<>();
        TreeNode cur = root;
        long pre = Long.MIN_VALUE;
        while (cur != null || !stack.isEmpty()){
            while (cur != null){
                stack.push(cur);
                cur = cur.left;
            }
            cur = stack.pop();
            if (cur.val > pre){
                pre = cur.val;
            } else {
                return false;
            }
            cur = cur.right;
        }
        return true;
    }
}
